<?php
class Faculty_User_Model extends CI_Model {
    //exp_fmis_member


	public function __construct(){
		parent::__construct();

        $this->load->database();
	}

    public function getById($id = 0)
    {
        $sql = "SELECT * FROM exp_fmis_member WHERE ID LIKE ?";
        $query =  $this->db->query($sql, $id);
        $result = $query->row_array();
        return $result;
    }


    public function get_userPass($username, $password)
    {

        $sql = "Select *
        FROM `exp_fmis_member_profile` Inner Join
        (`exp_fmis_member` INNER JOIN `exp_fmis_usergroup` ON exp_fmis_member.Usergroup = exp_fmis_usergroup.ID)
        ON MemberID = exp_fmis_member.ID
        WHERE MemberID = (SELECT exp_fmis_member.ID FROM `exp_fmis_member` WHERE Username like ?  AND Password like ?)";

        $data = $this->db->query($sql,array($username,$password));
        return array(
            'data' => $data->row(),
            'numRows' => $data->num_rows()

        );
    }

    public function updateStatusById($id, $status)
    {
        $sql = "UPDATE `exp_fmis_member`
        SET
        `Status` = ? WHERE ID = ?";

        $this->db->query($sql, array( $status, $id ));
    }


    public function updateStatus($id, $status)
    {
        $sql = "UPDATE `exp_fmis_member`
        SET
        `Status` = ?
         WHERE ID = ?";

        $this->db->query($sql, array(
            $status,
            $id
        ));
    }

    public function addMember($id, $username, $password, $status, $usergroup)
    {
        $sql = 'INSERT INTO `exp_fmis_member` (`ID`,`Username`, `Password`, `Status`, `Usergroup`)
        VALUES (?,?,?,?,?)';
        $this->db->query($sql,
        array(

            $id,
            $username,
            $password,
            $status,
            $usergroup
        ));
    }

    public function addMemberProfile(
        $memID,$rank,$degree,$spec,$dept,$fname,$mnname,$lname,$bday,$address)
    {
        $sql = 'INSERT INTO `exp_fmis_member_profile`(

        `MemberID`,
        `Rank`,
        `Degree`,
        `Specialty`,
        `Department`,
        `FirstName`,
        `MiddleName`,
        `LastName`,
        `Birthdate`,
        `Address`

        )
        VALUES (?,?,?,?,?,?,?,?,?,?)';

        $this->db->query($sql,
            array(

                $memID,
                $rank,
                $degree,
                $spec,
                $dept,
                $fname,
                $mnname,
                $lname,
                $bday,
                $address

            ));
    }

    public function getIdByUserPass($user, $pass)
    {
        $sql = 'SELECT `ID` FROM `exp_fmis_member`
        WHERE `Username` = ? AND `Password` = ?';

        $query = $this->db->query($sql,
            array(
                $user,
                $pass
            ));

        $result = $query->result();
        return $result;

    }

    public function getMaxMemberID()
    {
        $sql = 'SELECT MAX(ID) as "ID" FROM `exp_fmis_member`';
        $query = $this->db->query($sql);

        $result = $query->row_array();
        return $result['ID'];

    }

}
